package com.xiaoxi.flyweightmode.factory;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @Author xiaoxi
 * @ProjectName design_mode
 * @PackageName com.xiaoxi.flyweightmode.factory
 * @ClassName BigCharFactory
 * @Description TODO
 * @Date 2023/4/6 21:23
 * @Version 1.0
 */
public class BigCharFactory {

    private Map<String, BigChar> map = new HashMap<>();

    private static BigCharFactory singleton = new BigCharFactory();

    // 私有构造函数
    private BigCharFactory() {
    }

    // 获取唯一实例
    public static BigCharFactory getInstance() {
        return singleton;
    }

    // 获取bigchar工厂
    public synchronized BigChar getBigChar(char charName) {
        BigChar bigChar = map.get(charName + "");
        if (Objects.isNull(bigChar)) {
            bigChar = new BigChar(charName);
            map.put(charName + "", bigChar);
        }
        return bigChar;
    }

    // 获取bigchar工厂
    public synchronized BigChar getBigChar(char charName, boolean shared) {
        BigChar bigChar = new BigChar(charName);
        map.put(charName + "", bigChar);
        return bigChar;
    }

}
